<html>
 <head>
  <title>PHP Demo A2.6</title>
 </head>

<body>

<h1>Demo A2.6</h1>

<p>


<?php

//  phpinfo();

  echo "<p>" . `/bin/date` . "<p>";

// import the PHP code that make it so easy to access the remote server
  require('HttpClient.class.php');

// create a "client" object which will access the desired web site
  $client = new HttpClient('media.jcu.edu.au');

// leave debug off UNLESS you have problems
  $client->setDebug(false);

// grab a news item
  if ( ! $client->get('/story.cfm?id=777 ') ) {
    die( 'An error occurred: ' . $client->getError() );
  }

// copy the web page contents into $pageContents and display
  $pageContents = $client->getContent();

// Note that the item we want is between comment tags
//     <!-- start content -->
//     <!-- end content -->
//
// This is essentially a text extraction operation where these comments are
// the delimiters that surround the desired text.  I figured that regular
// expressions are going to provide a clean answer.
//
// On www.php.net, I did a function search for PHP "regexp" and then
// "preg_match" and decide to use preg_match.  Read the examples and, in
// particular, the item http://au3.php.net/preg_match_all
//
// Note that a regular expression convention is to use the first character
// of the control string be the delimiter of the pattern.  I do not see any
// ':' chars in the HTML of the above comment strings so I choose ':' as a
// delimiter.
//
// The following regexp pattern should select the text between the HTML
// comments i.e. the news item of interest:
//
//    ':<!-- start content -->(.*)<!-- end content -->:';
//
// where the text that we want to extract is represented inside parentheses.
// We have a .* inside the () characters so that means we want all characters
// between the two delimiters.

// As no text was extracted even though we know there is text between the
// delimiters, we have to do some debugging.  It turns out that we have
// neglected the fact that our desired text contains new lines and we need
// to use RE options to allow new lines into the target text.  Options can
// be placed at the end of this RE (after the final ':' char).  Here are a
// few of interest:
//
//   option s means a (.*) can match new lines;
//   option x means we ignore white space when trying to match;
//   option i says patterns match upper and lower case chars ... even though I
//            saw the TABLE token appear as uppercase only, I'll choose this
//            option to get more portability;
//   other options are described in item  PATTERN MODIFIERS  in the menu at
//            the top left of the documentation for preg_match_all, which is
//            http://au3.php.net/manual/en/reference.pcre.pattern.modifiers.php
//
// This leads to the final RE control string:

  $News_Item_Pattern = ':<!-- start content -->(.*)<!-- end content -->:is';

  $Nitems = preg_match_all( $News_Item_Pattern, $pageContents, $News_Items, PREG_PATTERN_ORDER );

  print 'Extracted ' . $Nitems . ' items.  A dump of the raw HTML follows';

  echo '<p> <hr>';
  print $News_Items[1][0];

//echo '<p> <hr> <pre>';
//
//  $html_dump=str_replace( '<', '&lt;', $News_Items[1][0] );
//  print $html_dump;
//
//echo '</pre>';
?>
 
<p>

<hr>

</body>
</html>
